const app = getApp()
const $api = require('../../utils/api.js').API;
Page({
    data: {
        imgUrl:app.globalData.imgUrl,
        loadmore:true,
        list:{},
        moneyArr:['面议','5k以下','5k~8k','8k~10k','10k以上'],
        keyword:'',
        page:1,
        pageShow:true,
    },
    //事件处理函数
    onShow: function (option){
        this.login()
    },
    search:function(e){
        var keyword = e.detail.value
        this.setData({
            keyword:keyword,
            pageShow:true,
            page:1
        })
        wx.showLoading()
        this.getCompany(keyword)
    },
    getCompany:function(keyword){
        var data = {'keyword':this.data.keyword,
                    'page':this.data.page}
        var that = this
        var list = this.data.list
        $api.post('company/search',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                if(res.data.data.total == 0){
                    that.setData({
                        loadmore:false
                    })
                }else{
                    that.setData({
                        loadmore:true
                    })
                }
                if(res.data.data.last_page <= that.data.page){
                    that.setData({
                        pageShow:false
                    })
                }
                wx.hideLoading()
                if(that.data.page == 1){
                    that.setData({
                        list:res.data.data.data
                    })
                }else{
                    that.setData({
                        list:list.concat(res.data.data.data)
                    })
                }
            }
        }).catch(err => {
            //请求失败
            //console.log('error','请求失败');
        })
    },
    jump:function(e){
        var id = e.currentTarget.dataset.id
        var role = wx.getStorageSync('role');
        if(role == 1){//未注册
            wx.navigateTo({
                url: '/pages/login/index'
            })
            return;
        }
        if(role == 3 || role == 4){//机构和代理商
            wx.showToast({
                title:'暂无权限',
                duration:3000,
                mask:true,
                icon:'none', 
            })
            return;
        }
        wx.navigateTo({
            url: '/pages/company/info?id='+id
        })
    },
    onReachBottom: function(t){
        if(this.data.pageShow){
            //当前不是最后一页可加载
            this.setData({
                page: this.data.page + 1,
            })
            wx.showLoading()
            this.getCompany();
        }
    },
    onShareAppMessage: function() {
      return {
          title: "天意美业直通车",
          path: "/pages/index/index?puid="+wx.getStorageSync('uid')
      };
    },
    login:function(){
        if(!wx.getStorageSync('uid')){
          //没有缓存 跳转授权页面
          wx.navigateTo({
            url:'/pages/index/guide'
          })
        }else{
          //验证手机号码是否存在
          this.checkphone();
        }
      },
      //查询手机号  是否注册
      checkphone:function(){
        var data = {
          'uid':wx.getStorageSync('uid')
        }
        var that = this
        $api.post('member/phone',data).then(res =>{
          if(res.data.code == 1){
            
          }else{
            wx.navigateTo({
              url:'/pages/index/phone'
            })
          }
        })
      }
  })
  